﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using Data.Infrastructure;
using Data.Repositories;
using Domain;
using Domain.Entities;
using Moq;
using Ninject;
using Service;

namespace Web.Infrastructure
{
    public class NinjectControllerFactory: DefaultControllerFactory
    {

            private IKernel ninjectKernel;

            public NinjectControllerFactory() {
                ninjectKernel = new StandardKernel();
                AddBindings();
            }

            protected override IController GetControllerInstance(RequestContext
                requestContext, Type controllerType) {

                return controllerType == null ? null : (IController)ninjectKernel.Get(controllerType);
            }

            private void AddBindings() {

                Mock<IProductsRepository> mock = new Mock<IProductsRepository>();

                mock.Setup(m => m.Products).Returns(new List<Product> {
                    new Product { Name = "Football", Price = 25 },
                    new Product { Name = "Surf board", Price = 179 },
                    new Product { Name = "Running shoes", Price = 95 }
                }.AsQueryable());
                //ninjectKernel.Bind<IProductsRepository>().ToConstant(mock.Object);

                ninjectKernel.Bind<IDatabaseFactory>().To<DatabaseFactory>();
                ninjectKernel.Bind<IUnitOfWork>().To<UnitOfWork>();

                ninjectKernel.Bind<IProductsService<Product, PageData<Product>>>()
                                         .To<ProductsService<Product, PageData<Product>>>();
                ninjectKernel.Bind<IProductsRepository>().To<ProductsRepository>();
            

            }
         }
    }